package com.iqismart.shorturl.controller.api.url;

import com.iqismart.shorturl.cache.UrlCountCache;
import com.iqismart.shorturl.service.UrlService;
import com.iqismart.shorturl.vo.CheckUrlRequest;
import com.iqismart.shorturl.vo.CheckUrlResponse;
import com.iqismart.shorturl.vo.EaddRequest;
import com.iqismart.shorturl.vo.EaddResponse;
import com.iqismart.shorturl.vo.base.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @Auther sty
 * @createTime 2019/10/14 01:32
 */
@RestController
@RequestMapping("url")
@CrossOrigin
public class UrlController {

    @Autowired
    private UrlService urlService;
    /**
     *
     * @param request "longurl":"baidu.com","days":"","count":"","password":"","tarea":""
     * @return {"status":"200","message":"ok","data":{"d":"http://u1rl.cn/uAXbA"}}
     */
    @RequestMapping("eadd")
    public Result<EaddResponse> eadd(@RequestBody EaddRequest request){
        EaddResponse response =  urlService.eadd(request);
        return new Result(response);
    }

    @RequestMapping("checkurl")
    public Result<CheckUrlResponse> checkurl(@RequestBody CheckUrlRequest request){
        CheckUrlResponse response =  urlService.checkurl(request);
        return new Result(response);
    }

    @RequestMapping("count")
    public Result count(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("today_add", UrlCountCache.todayAdd);
        jsonObject.put("today_get",UrlCountCache.todayGet);
        jsonObject.put("total_add",UrlCountCache.totalAdd.longValue()+7580000);
        jsonObject.put("total_get",UrlCountCache.totalGet.longValue()+260000);
        return new Result(jsonObject);
    }
}
